package br.edu.ufcg.lsd.wbis.control;

import java.sql.Date;
import java.util.List;

import br.edu.ufcg.lsd.wbis.control.allocation.AllocationManager;
import br.edu.ufcg.lsd.wbis.control.equipment.Computer;
import br.edu.ufcg.lsd.wbis.control.equipment.ComputerType;
import br.edu.ufcg.lsd.wbis.control.equipment.DiscDriver;
import br.edu.ufcg.lsd.wbis.control.equipment.Display;
import br.edu.ufcg.lsd.wbis.control.equipment.Equipment;
import br.edu.ufcg.lsd.wbis.control.equipment.EquipmentManager;
import br.edu.ufcg.lsd.wbis.control.equipment.HardDisk;
import br.edu.ufcg.lsd.wbis.control.equipment.Keyboard;
import br.edu.ufcg.lsd.wbis.control.equipment.Memory;
import br.edu.ufcg.lsd.wbis.control.equipment.Mouse;
import br.edu.ufcg.lsd.wbis.control.equipment.NoBreak;
import br.edu.ufcg.lsd.wbis.control.equipment.PowerSource;
import br.edu.ufcg.lsd.wbis.control.equipment.Printer;
import br.edu.ufcg.lsd.wbis.control.equipment.Processor;
import br.edu.ufcg.lsd.wbis.control.equipment.Router;
import br.edu.ufcg.lsd.wbis.control.equipment.RouterType;
import br.edu.ufcg.lsd.wbis.control.equipment.VideoCard;
import br.edu.ufcg.lsd.wbis.control.exception.AuthenticationFailedException;
import br.edu.ufcg.lsd.wbis.control.exception.BusinessValidationException;
import br.edu.ufcg.lsd.wbis.control.exception.CannotOpenDatabaseSessionException;
import br.edu.ufcg.lsd.wbis.control.exception.DuplicatedIdentifierException;
import br.edu.ufcg.lsd.wbis.control.exception.InvalidComponentInventoryCode;
import br.edu.ufcg.lsd.wbis.control.exception.InvalidComputerInventoryCode;
import br.edu.ufcg.lsd.wbis.control.exception.PermissionDeniedException;
import br.edu.ufcg.lsd.wbis.control.exception.RootUserAlreadyCreatedException;
import br.edu.ufcg.lsd.wbis.control.exception.RootUserCreationCancelledException;
import br.edu.ufcg.lsd.wbis.control.exception.UnknownEntityException;
import br.edu.ufcg.lsd.wbis.control.exception.UserNotAuthorizedException;
import br.edu.ufcg.lsd.wbis.control.history.HistoryManager;
import br.edu.ufcg.lsd.wbis.control.report.ReportManager;
import br.edu.ufcg.lsd.wbis.control.user.User;
import br.edu.ufcg.lsd.wbis.control.user.UserLevel;
import br.edu.ufcg.lsd.wbis.control.user.UserManager;
import br.edu.ufcg.lsd.wbis.control.user.UserValidator;
import br.edu.ufcg.lsd.wbis.control.util.matching.Matcher;

/**
 * This is the main controller of the system. All WBIS functionalities must be
 * accessed through this classes' methods.
 * 
 * @author Renato Miceli
 * @see WBISFacade
 */
public class WBISController implements WBISFacade {

	/**
	 * The user's manager.
	 */
	private final UserManager userManager;

	/**
	 * Root user's creation status.
	 */
	private RootUserStatus rootUserStatus;

	/**
	 * The equipment's manager.
	 */
	private final EquipmentManager equipmentManager;

	@SuppressWarnings("unused")
	private final AllocationManager allocationManager;

	@SuppressWarnings("unused")
	private final HistoryManager historyManager;

	@SuppressWarnings("unused")
	private final ReportManager reportManager;

	public WBISController() throws CannotOpenDatabaseSessionException {
		this.userManager = new UserManager();
		this.equipmentManager = new EquipmentManager();
		this.allocationManager = new AllocationManager();
		this.historyManager = new HistoryManager();
		this.reportManager = new ReportManager();

		this.rootUserStatus = RootUserStatus.NOT_CREATED;
	}

	// ///////////////////////
	// User Manager Methods //
	// ///////////////////////

	/**
	 * {@inheritDoc}
	 */
	@Override
	public User addUser(String requesterLogin, String requesterPassword, String userLogin, String userPassword, String userName, String userEmail, UserLevel level, boolean authorized)
			throws BusinessValidationException, DuplicatedIdentifierException, PermissionDeniedException, AuthenticationFailedException,
			UserNotAuthorizedException {

		User actor = this.authenticateUser(requesterLogin, requesterPassword);
		this.checkPermissions(actor, userLogin);

		return addUser(userLogin, userPassword, userName, userEmail, authorized, level);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public User addCommonUser(String login, String password, String userName, String email) throws BusinessValidationException,
			DuplicatedIdentifierException {

		return addUser(login, password, userName, email, false, UserLevel.COMMON_USER);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public User addRootUser(String login, String password, String userName, String email) throws BusinessValidationException,
			DuplicatedIdentifierException, RootUserCreationCancelledException, RootUserAlreadyCreatedException {

		if (this.rootUserStatus.equals(RootUserStatus.CREATED)) {
			throw new RootUserAlreadyCreatedException();
		}
		if (this.rootUserStatus.equals(RootUserStatus.CANCELLED)) {
			throw new RootUserCreationCancelledException();
		}

		User rootUser = addUser(login, password, userName, email, true, UserLevel.ADMIN);
		this.rootUserStatus = RootUserStatus.CREATED;
		return rootUser;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void forbidRootUserCreation() throws RootUserAlreadyCreatedException {
		if (this.rootUserStatus.equals(RootUserStatus.CREATED)) {
			throw new RootUserAlreadyCreatedException();
		}
		this.rootUserStatus = RootUserStatus.CANCELLED;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public RootUserStatus getRootUserStatus() {
		return this.rootUserStatus;
	}

	private User addUser(String userLogin, String userPassword, String userName, String userEmail, boolean authorized, UserLevel userLevel)
			throws BusinessValidationException, DuplicatedIdentifierException {

		UserValidator.validateUser(userLogin, userPassword, userName, userEmail, userLevel);

		userLogin = userLogin.trim();
		userPassword = userPassword.trim();
		userName = userName.trim();
		userEmail = userEmail.trim();

		return this.userManager.addUser(userLogin, userPassword, userName, userEmail, authorized, userLevel);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void removeUser(String login, String password, String loginToRemove) throws BusinessValidationException, UnknownEntityException,
			PermissionDeniedException, AuthenticationFailedException, UserNotAuthorizedException {

		User actor = this.authenticateUser(login, password);
		this.checkPermissions(actor, loginToRemove);

		UserValidator.validateUserLogin(loginToRemove);

		loginToRemove = loginToRemove.trim();
		this.userManager.removeUser(loginToRemove);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void updateUserInfo(String login, String password, String loginToUpdate, String newUserName, String newEmail, UserLevel newLevel,
			boolean authorizationStatus) throws BusinessValidationException, UnknownEntityException, AuthenticationFailedException,
			UserNotAuthorizedException, PermissionDeniedException {

		User actor = this.authenticateUser(login, password);
		this.checkPermissions(actor, loginToUpdate);
		
		if (!actor.getLevel().isAdmin() && (newLevel.equals(UserLevel.ADMIN) || !authorizationStatus)) {
			throw new PermissionDeniedException();
		}

		UserValidator.validateUserLogin(loginToUpdate);
		UserValidator.validateUserName(newUserName);
		UserValidator.validateUserEmail(newEmail);
		UserValidator.validateUserLevel(newLevel);

		loginToUpdate = loginToUpdate.trim();
		newUserName = newUserName.trim();
		newEmail = newEmail.trim();

		this.userManager.updateUserInfo(loginToUpdate, newUserName, newEmail, newLevel, authorizationStatus);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void updateUserPassword(String login, String password, String loginToUpdate, String newPassword) throws BusinessValidationException,
			AuthenticationFailedException, UserNotAuthorizedException, PermissionDeniedException, UnknownEntityException {

		User actor = this.authenticateUser(login, password);
		this.checkPermissions(actor, loginToUpdate);

		UserValidator.validateUserLogin(loginToUpdate);
		UserValidator.validateUserPassword(newPassword);

		loginToUpdate = loginToUpdate.trim();
		newPassword = newPassword.trim();

		this.userManager.updateUserPassword(loginToUpdate, newPassword);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void authorizeUser(String myLogin, String myPwd, String login, boolean authorize) throws UnknownEntityException,
			BusinessValidationException, AuthenticationFailedException, UserNotAuthorizedException, PermissionDeniedException {
		User actor = this.authenticateUser(myLogin, myPwd);

		if (!actor.getLevel().isAdmin()) {
			throw new PermissionDeniedException();
		}
		this.userManager.authorizeUser(login, authorize);
	}

	/**
	 * Checks whether the given actor is an administrator or has the same login
	 * as the one given. Otherwise, an exception is thrown.
	 * 
	 * @param actor
	 *            the checking actor.
	 * @param loginToHandle
	 *            the login to hangle.
	 * @throws PermissionDeniedException
	 *             in case either the actor is not an administrator or its login
	 *             is not the same as the given login to handle.
	 */
	private void checkPermissions(User actor, String loginToHandle) throws PermissionDeniedException {
		if (!actor.getLevel().isAdmin() && !actor.getLogin().equals(loginToHandle)) {
			throw new PermissionDeniedException();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public User authenticateUser(String login, String password) throws BusinessValidationException, AuthenticationFailedException,
			UserNotAuthorizedException {

		UserValidator.validateUserLogin(login);
		UserValidator.validateUserPassword(password);

		login = login.trim();

		User user;
		try {
			user = this.userManager.getUser(login);
		} catch (UnknownEntityException e) {
			throw new AuthenticationFailedException();
		}

		if (!user.checkPassword(password)) {
			throw new AuthenticationFailedException();
		}

		if (!user.isAuthorized()) {
			throw new UserNotAuthorizedException();
		}

		return user;
	}

	/**
	 * {@inheritDoc}
	 * @throws BusinessValidationException 
	 * @throws PermissionDeniedException 
	 * @throws UserNotAuthorizedException 
	 * @throws AuthenticationFailedException 
	 * @throws UserNotAuthorizedException 
	 * @throws AuthenticationFailedException 
	 * @throws UnknownEntityException 
	 */
	@Override
	public User getUser(String login, String password, String desiredLogin) throws BusinessValidationException, PermissionDeniedException, AuthenticationFailedException, UserNotAuthorizedException, UnknownEntityException {
	
		UserValidator.validateUserLogin(login);

		User actor = this.authenticateUser(login, password);
		checkPermissions(actor, desiredLogin);
		
		if(login.equals(desiredLogin)){
			return actor;
		}
		
		return this.userManager.getUser(desiredLogin);
	}

	/**
	 * {@inheritDoc}
	 * @throws UserNotAuthorizedException, PermissionDeniedException 
	 * @throws AuthenticationFailedException 
	 * @throws BusinessValidationException 
	 */
	@Override
	public List<User> getUsers(String login, String password) throws BusinessValidationException, AuthenticationFailedException, UserNotAuthorizedException, PermissionDeniedException {
		
		User actor = this.authenticateUser(login, password);
		
		if (!actor.getLevel().isAdmin()) {
			throw new PermissionDeniedException();
		}
		
		return this.userManager.getUsers();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<User> searchUsers(String login, String password, Matcher<User> userMatcher) throws BusinessValidationException,
			AuthenticationFailedException, UserNotAuthorizedException, PermissionDeniedException {
		
		checkUserPermissions(login, password);

		return this.userManager.searchUsers(userMatcher);
	}

	private void checkUserPermissions(String login, String password) throws BusinessValidationException, AuthenticationFailedException,
			UserNotAuthorizedException, PermissionDeniedException {

		User user = this.authenticateUser(login, password);
		if (!user.getLevel().equals(UserLevel.ADMIN)) {
			throw new PermissionDeniedException();
		}
	}

	// ////////////////////////////
	// Equipment Manager Methods //
	// ////////////////////////////

	@Override
	public Computer addComputer(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean alocable, boolean available, ComputerType computerType, String ownerLogin, int numProcessors,
			int numHDs) throws UserNotAuthorizedException, BusinessValidationException, DuplicatedIdentifierException, AuthenticationFailedException,
			PermissionDeniedException, UnknownEntityException {

		checkUserPermissions(login, password);

		User owner = userManager.getUser(ownerLogin);

		return equipmentManager.addComputer(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, alocable, available,
				computerType, owner, numProcessors, numHDs);
	}

	@Override
	public DiscDriver addDiscDriver(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean alocable, boolean available, boolean external, boolean recorder, String mediaTypes)
			throws UserNotAuthorizedException, BusinessValidationException, DuplicatedIdentifierException, AuthenticationFailedException,
			PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addDiscDriver(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, alocable, available,
				external, recorder, mediaTypes);
	}

	@Override
	public Display addDisplay(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, boolean datashow, String type, String resolution)
			throws UserNotAuthorizedException, BusinessValidationException, DuplicatedIdentifierException, AuthenticationFailedException,
			PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addDisplay(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, datashow, type,
				resolution);
	}

	@Override
	public HardDisk addHardDisk(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean alocable, boolean available, boolean external, String useDescription, int capacity, String bus)
			throws UserNotAuthorizedException, BusinessValidationException, DuplicatedIdentifierException, AuthenticationFailedException,
			PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addHardDisk(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, alocable, available,
				external, useDescription, capacity, bus);
	}

	@Override
	public Keyboard addKeyboard(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, String tipo) throws UserNotAuthorizedException, BusinessValidationException,
			DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addKeyboard(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, tipo);
	}

	@Override
	public Memory addMemory(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate, String model,
			String localization, boolean available, int capacity, int frequency, String bus) throws UserNotAuthorizedException,
			BusinessValidationException, DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addMemory(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, capacity,
				frequency, bus);
	}

	@Override
	public Mouse addMouse(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate, String model,
			String localization, boolean available, boolean optical) throws UserNotAuthorizedException, BusinessValidationException,
			DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addMouse(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, optical);
	}

	@Override
	public NoBreak addNoBreak(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, int capacity) throws UserNotAuthorizedException, BusinessValidationException,
			DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addNoBreak(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, capacity);
	}

	@Override
	public Equipment addOtherEquipment(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean alocable, boolean available, String type) throws UserNotAuthorizedException,
			BusinessValidationException, DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addOtherEquipment(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, alocable, available,
				type);
	}

	@Override
	public PowerSource addPowerSource(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, int power) throws UserNotAuthorizedException, BusinessValidationException,
			DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addPowerSource(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, power);
	}

	@Override
	public Printer addPrinter(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, boolean colored) throws UserNotAuthorizedException, BusinessValidationException,
			DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addPrinter(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, colored);
	}

	@Override
	public Processor addProcessor(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, String frequency, String l2Cache, int cores) throws UserNotAuthorizedException,
			BusinessValidationException, DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addProcessor(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, frequency,
				l2Cache, cores);
	}

	@Override
	public Router addRouter(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate, String model,
			String localization, boolean available, RouterType routerType, String vlan, int ports) throws UserNotAuthorizedException,
			BusinessValidationException, DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		return equipmentManager.addRouter(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, routerType, vlan,
				ports);
	}

	@Override
	public VideoCard addVideoCard(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, int memory, int frequency, String bus) throws UserNotAuthorizedException,
			BusinessValidationException, DuplicatedIdentifierException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.addVideoCard(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, memory,
				frequency, bus);
	}

	@Override
	public List<Equipment> getAllEquipments(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getAllEquipments();
	}

	@Override
	public List<Computer> getComputers(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getComputers();
	}

	@Override
	public List<DiscDriver> getDiscDrivers(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getDiscDrivers();
	}

	@Override
	public List<Display> getDisplays(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getDisplays();
	}

	@Override
	public List<HardDisk> getHardDisks(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getHardDisks();
	}

	@Override
	public List<Keyboard> getKeyboards(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getKeyboards();
	}

	@Override
	public List<Memory> getMemories(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getMemories();
	}

	@Override
	public List<Mouse> getMouses(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getMouses();
	}

	@Override
	public List<NoBreak> getNoBreaks(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getNoBreaks();
	}

	@Override
	public List<Equipment> getOtherEquipments(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getOtherEquipments();
	}

	@Override
	public List<PowerSource> getPowerSources(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getPowerSources();
	}

	@Override
	public List<Printer> getPrinters(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getPrinters();
	}

	@Override
	public List<Processor> getProcessors(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getProcessors();
	}

	@Override
	public List<Router> getRouters(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getRouters();
	}

	@Override
	public List<VideoCard> getVideoCards(String login, String password) throws UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);
		return equipmentManager.getVideoCards();
	}

	@Override
	public void removeEquipment(String login, String password, String inventoryCode) throws UserNotAuthorizedException, BusinessValidationException,
			UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.removeEquipment(inventoryCode);
	}

	@Override
	public void updateComputer(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean alocable, boolean available, ComputerType computerType, String ownerLogin, int numProcessors,
			int numHDs) throws UserNotAuthorizedException, BusinessValidationException, UnknownEntityException, AuthenticationFailedException,
			PermissionDeniedException {
		checkUserPermissions(login, password);

		User owner = userManager.getUser(ownerLogin);

		equipmentManager.updateComputer(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, alocable, available,
				computerType, owner, numProcessors, numHDs);
	}

	@Override
	public void updateDiscDriver(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean alocable, boolean available, boolean external, boolean recorder, String mediaTypes)
			throws UserNotAuthorizedException, BusinessValidationException, UnknownEntityException, AuthenticationFailedException,
			PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateDiscDriver(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, alocable, available,
				external, recorder, mediaTypes);

	}

	@Override
	public void updateDisplay(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, boolean datashow, String type, String resolution)
			throws UserNotAuthorizedException, BusinessValidationException, UnknownEntityException, AuthenticationFailedException,
			PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateDisplay(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, datashow, type,
				resolution);
	}

	@Override
	public void updateHardDisk(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean alocable, boolean available, boolean external, String useDescription, int capacity, String bus)
			throws UserNotAuthorizedException, BusinessValidationException, UnknownEntityException, AuthenticationFailedException,
			PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateHardDisk(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, alocable, available, external,
				useDescription, capacity, bus);
	}

	@Override
	public void updateKeyboard(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, String tipo) throws UserNotAuthorizedException, BusinessValidationException,
			UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateKeyboard(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, tipo);
	}

	@Override
	public void updateMemory(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, int capacity, int frequency, String bus) throws UserNotAuthorizedException,
			BusinessValidationException, UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateMemory(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, capacity, frequency,
				bus);
	}

	@Override
	public void updateMouse(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate, String model,
			String localization, boolean available, boolean optical) throws UserNotAuthorizedException, BusinessValidationException,
			UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateMouse(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, optical);
	}

	@Override
	public void updateNoBreak(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, int capacity) throws UserNotAuthorizedException, BusinessValidationException,
			UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateNoBreak(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, capacity);
	}

	@Override
	public void updateOtherEquipment(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean alocable, boolean available, String type) throws UserNotAuthorizedException,
			BusinessValidationException, UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager
				.updateOtherEquipment(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, alocable, available, type);

	}

	@Override
	public void updatePowerSource(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, int power) throws UserNotAuthorizedException, BusinessValidationException,
			UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updatePowerSource(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, power);
	}

	@Override
	public void updatePrinter(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, boolean colored) throws UserNotAuthorizedException, BusinessValidationException,
			UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updatePrinter(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, colored);

	}

	@Override
	public void updateProcessor(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, String frequency, String l2Cache, int cores) throws UserNotAuthorizedException,
			BusinessValidationException, UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateProcessor(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, frequency, l2Cache,
				cores);
	}

	@Override
	public void updateRouter(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, RouterType routerType, String vlan, int ports) throws UserNotAuthorizedException,
			BusinessValidationException, UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateRouter(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, routerType, vlan,
				ports);
	}

	@Override
	public void updateVideoCard(String login, String password, String inventoryCode, String brand, String serialCode, Date acquisitionDate,
			String model, String localization, boolean available, int memory, int frequency, String bus) throws UserNotAuthorizedException,
			BusinessValidationException, UnknownEntityException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.updateVideoCard(inventoryCode, brand, serialCode, acquisitionDate, model, localization, null, available, memory, frequency,
				bus);

	}

	@Override
	public void addComponentToComputer(String login, String password, String computerCode, String componentCode) throws InvalidComputerInventoryCode,
			InvalidComponentInventoryCode, UserNotAuthorizedException, BusinessValidationException, AuthenticationFailedException,
			PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.addComponentToComputer(computerCode, componentCode);
	}

	@Override
	public void addHostNameToEquipment(String login, String password, String equipmentCode, String hostname) throws UnknownEntityException,
			UserNotAuthorizedException, BusinessValidationException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.addHostNameToEquipment(equipmentCode, hostname);
	}

	@Override
	public void addIPAddressToEquipment(String login, String password, String equipmentCode, String ipAdress) throws UnknownEntityException,
			UserNotAuthorizedException, BusinessValidationException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.addIPAddressToEquipment(equipmentCode, ipAdress);
	}

	@Override
	public void addMacAddressToEquipment(String login, String password, String equipmentCode, String macAddress) throws UnknownEntityException,
			UserNotAuthorizedException, BusinessValidationException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.addMacAddressToEquipment(equipmentCode, macAddress);
	}

	@Override
	public void addServiceToComputer(String login, String password, String computerCode, String service) throws InvalidComputerInventoryCode,
			UserNotAuthorizedException, BusinessValidationException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.addServiceToComputer(computerCode, service);
	}

	@Override
	public void removeComponentFromComputer(String login, String password, String computerCode, String componentCode)
			throws InvalidComputerInventoryCode, InvalidComponentInventoryCode, UserNotAuthorizedException, BusinessValidationException,
			AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.removeComponentFromComputer(computerCode, componentCode);
	}

	@Override
	public void removeHostNameFromEquipment(String login, String password, String inventoryID, String hostname) throws UnknownEntityException,
			UserNotAuthorizedException, BusinessValidationException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.removeHostNameFromEquipment(inventoryID, hostname);
	}

	@Override
	public void removeIPAddressFromEquipment(String login, String password, String equipmentCode, String ipAdress) throws UnknownEntityException,
			UserNotAuthorizedException, BusinessValidationException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.removeIPAddressFromEquipment(equipmentCode, ipAdress);
	}

	@Override
	public void removeMacAddressFromEquipment(String login, String password, String equipmentCode, String macAddress) throws UnknownEntityException,
			UserNotAuthorizedException, BusinessValidationException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.removeMacAddressFromEquipment(equipmentCode, macAddress);
	}

	@Override
	public void removeServiceFromComputer(String login, String password, String computerCode, String service) throws InvalidComputerInventoryCode,
			UserNotAuthorizedException, BusinessValidationException, AuthenticationFailedException, PermissionDeniedException {
		checkUserPermissions(login, password);

		equipmentManager.removeServiceFromComputer(computerCode, service);
	}
}